import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:kudos/core/utils/kudo_colors.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';

/// 加载动画组件
class LoadingWidget extends StatelessWidget {
  final String? message;
  final double size;
  final Color color;
  final LoadingAnimationType animationType;

  const LoadingWidget({
    super.key,
    this.message,
    this.size = 50.0,
    this.color = Colors.blue,
    this.animationType = LoadingAnimationType.threeRotatingDots,
  });

  @override
  Widget build(BuildContext context) {
    return [
      // 加载动画
      _buildLoadingAnimation(),

      // 加载文字
      if (message != null)
        Text(message!)
            .fontSize(14.sp)
            .textColor(Colors.grey.shade600)
            .fontWeight(FontWeight.w500)
            .paddingTop(16.h),
    ].toColumn(mainAxisAlignment: MainAxisAlignment.center).center();
  }

  /// 构建加载动画
  Widget _buildLoadingAnimation() {
    switch (animationType) {
      case LoadingAnimationType.threeRotatingDots:
        return LoadingAnimationWidget.threeRotatingDots(
          color: color,
          size: size.w,
        );
      case LoadingAnimationType.staggeredDotsWave:
        return LoadingAnimationWidget.staggeredDotsWave(
          color: color,
          size: size.w,
        );
      case LoadingAnimationType.fourRotatingDots:
        return LoadingAnimationWidget.fourRotatingDots(
          color: color,
          size: size.w,
        );
      case LoadingAnimationType.bouncingBall:
        return LoadingAnimationWidget.bouncingBall(color: color, size: size.w);
      case LoadingAnimationType.flickr:
        return LoadingAnimationWidget.flickr(
          leftDotColor: color,
          rightDotColor: color.withValues(alpha: 0.6),
          size: size.w,
        );
      case LoadingAnimationType.inkDrop:
        return LoadingAnimationWidget.inkDrop(color: color, size: size.w);
      case LoadingAnimationType.twistingDots:
        return LoadingAnimationWidget.twistingDots(
          leftDotColor: color,
          rightDotColor: color.withValues(alpha: 0.6),
          size: size.w,
        );
      case LoadingAnimationType.threeArchedCircle:
        return LoadingAnimationWidget.threeArchedCircle(
          color: color,
          size: size.w,
        );
    }
  }

  /// 显示加载对话框
  static void showLoadingDialog(
    BuildContext context, {
    String? message,
    double size = 50.0,
    Color color = KudoColors.green,
    LoadingAnimationType animationType = LoadingAnimationType.threeRotatingDots,
    bool barrierDismissible = false,
  }) {
    showDialog(
      context: context,
      builder:
          (context) => Dialog(
            backgroundColor: Colors.transparent,
            child: Container(
              width: 200.w,
              height: 200.h,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12.r),
              ),
              child: LoadingWidget(
                message: message ?? 'Processing...',
                size: 60,
                color: color,
                animationType: animationType,
              ),
            ),
          ),
    );
  }

  /// 隐藏加载对话框
  static void hideLoadingDialog(BuildContext context) {
    Navigator.of(context).pop();
  }
}

/// 加载动画类型枚举
enum LoadingAnimationType {
  threeRotatingDots,
  staggeredDotsWave,
  fourRotatingDots,
  bouncingBall,
  flickr,
  inkDrop,
  twistingDots,
  threeArchedCircle,
}
